package com.yh.wuye.view;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class Todo extends BaseActivity
{

  private Button btnSelect;
  private Button btnRefresh;
  private ListView lvTodo;

  private TodoAdapter mTodoAdapter; // 查看标签明细适配器

  private TodoTypeAdapter mTodoTypeAdapter; // 查看标签明细适配器
  private AlertDialog.Builder mSelectTodoTypeDlg; // 查看标签明细选择对话框

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.todo);

    initView();
    setData();
  }


  @Override
  protected void initView()
  {
    // TODO Auto-generated method stub
    super.initView();
    btnSelect = (Button) findViewById(R.id.btnSelect);
    btnSelect.setOnClickListener(onSelect);

    btnRefresh = (Button) findViewById(R.id.btnRefresh);

    lvTodo = (ListView) findViewById(R.id.lvTodo);
  }

  private void setData()
  {
    mTodoAdapter = new TodoAdapter();

    TodoItem todo1 = new TodoItem();
    todo1.id = "1";
    todo1.name = "维修任务1";
    todo1.type = "1";
    mTodoAdapter.addItem(todo1);

    TodoItem todo2 = new TodoItem();
    todo2.id = "2";
    todo2.name = "服务任务1";
    todo2.type = "2";
    mTodoAdapter.addItem(todo2);

    lvTodo.setAdapter(mTodoAdapter);

    mTodoTypeAdapter = new TodoTypeAdapter();

    SimpleItem item1 = new SimpleItem();
    item1.id = "1";
    item1.name = "维修";
    mTodoTypeAdapter.addItem(item1);

    SimpleItem item2 = new SimpleItem();
    item2.id = "2";
    item2.name = "服务";
    mTodoTypeAdapter.addItem(item2);

    SimpleItem item3 = new SimpleItem();
    item3.id = "0";
    item3.name = "显示所有";
    mTodoTypeAdapter.addItem(item3);
  }

  private OnClickListener onSelect = new OnClickListener()
  {

    @Override
    public void onClick(View v)
    {
      // TODO Auto-generated method stub

      // 查询标签信息
      if (!mTodoTypeAdapter.isEmpty())
      {
        if (mSelectTodoTypeDlg == null)
        {
          mSelectTodoTypeDlg = new AlertDialog.Builder(ctx);
          mSelectTodoTypeDlg.setTitle("请选择待办类型");
          mSelectTodoTypeDlg.setAdapter(mTodoTypeAdapter, onTodoType);
          mSelectTodoTypeDlg.setNegativeButton("取消", null);
        }
        mSelectTodoTypeDlg.show();
      };
    }

    android.content.DialogInterface.OnClickListener onTodoType = new android.content.DialogInterface.OnClickListener()
    {
      @Override
      public void onClick(DialogInterface dialog, int which)
      {
        SimpleItem item = (SimpleItem) mTodoTypeAdapter.getItem(which);
        btnSelect.setText(item.name);

        mTodoAdapter.type = item.id;
        mTodoTypeAdapter.notifyDataSetChanged();
      }

    };
  };

  private class TodoAdapter extends BaseAdapter
  {
    public ArrayList<TodoItem> items;
    public String type;

    public TodoAdapter()
    {
      super();
      items = new ArrayList<TodoItem>();
      type = "0";
    }

    public void addItem(TodoItem todoItem)
    {
      items.add(todoItem);
    }

    @Override
    public int getCount()
    {
      return items.size();
    }

    @Override
    public Object getItem(int position)
    {
      return position;
    }

    @Override
    public long getItemId(int position)
    {
      return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
      TodoItem item = items.get(position);
      if ((!"0".endsWith(type)) && (!type.endsWith(item.type)))
        return null;

      Holder holder;
      if (convertView == null)
      {
        convertView = LayoutInflater.from(ctx).inflate(R.layout.todo_item, null);
        holder = new Holder();
        holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
        holder.btnFinish = (Button) convertView.findViewById(R.id.btnFinish);
        holder.btnAssign = (Button) convertView.findViewById(R.id.btnAssign);

        convertView.setTag(holder);
      } else
        holder = (Holder) convertView.getTag();

      holder.tvName.setText(item.name);

      return convertView;
    }

    private class Holder
    {
      public TextView tvName;
      public Button btnFinish;
      public Button btnAssign;
    }
  }

  private class TodoItem
  {
    public String id;
    public String type;
    public String name;
  }

  private class TodoTypeAdapter extends BaseAdapter
  {
    public ArrayList<SimpleItem> items;

    public TodoTypeAdapter()
    {
      super();
      items = new ArrayList<SimpleItem>();
    }

    public void addItem(SimpleItem simpleItem)
    {
      items.add(simpleItem);
    }

    @Override
    public int getCount()
    {
      return items.size();
    }

    @Override
    public Object getItem(int position)
    {
      return items.get(position);
    }

    @Override
    public long getItemId(int position)
    {
      return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
      SimpleItem item = items.get(position);

      Holder holder;
      if (convertView == null)
      {
        convertView = LayoutInflater.from(ctx).inflate(R.layout.simple_item, null);
        holder = new Holder();
        holder.tvName = (TextView) convertView.findViewById(R.id.tvName);

        convertView.setTag(holder);
      } else
        holder = (Holder) convertView.getTag();

      holder.tvName.setText(item.name);

      return convertView;
    }

    private class Holder
    {
      public TextView tvName;
    }
  }

  private class SimpleItem
  {
    public String id;
    public String name;
  }
}
